package com.bgy.model;

import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 授权获取到的用户信息
 * @author Hegx
 */
public class Mmsryxx extends Model<Mmsryxx> {
	private static final long serialVersionUID = 6204222383226990020L;
	static Log log = Log.getLog(Mmsryxx.class);	
	public static final Mmsryxx me = new Mmsryxx();
	
	public boolean save(Object userid,Object name,Object department,Object position,Object mobile,
			Object gender,Object avatar,Object status){
		log.info("同步注册用户信息: userid:"+userid+" name:"+name+" mobile:"+mobile);		
		Mmsryxx user = findByUserid((String)userid);
		if (user!=null) {
			user.set("name", name);
			user.set("department", department);
			user.set("position", position);
			user.set("mobile", mobile);		
			user.set("gender", gender);	
			user.set("avatar", avatar);	
			user.set("status", status);	
			return user.update();
		}else {
			if (StrKit.notBlank(userid.toString())) {
				Mmsryxx muser = new Mmsryxx();
				muser.set("userid", userid);
				muser.set("name", name);
				muser.set("department", department);
				muser.set("position", position);
				muser.set("mobile", mobile);		
				muser.set("gender", gender);	
				muser.set("avatar", avatar);	
				muser.set("status", status);				
				return muser.save();
			}
		}
		return false;
	}
	
	public List<Mmsryxx> getAll(){
		return me.find("select * from mms_ryxx");
	}
	
	public Page<Mmsryxx> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", " from mms_ryxx order by xh asc");
	}

	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */	
	public Mmsryxx findByUserid(String userid){		
		return this.findFirst("select * from mms_ryxx where userid=?", userid);
	}	

}

